package com.ph.shopping.facade.order.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.dubbo.config.annotation.Service;
import com.ph.shopping.common.core.base.BaseService;
import com.ph.shopping.common.core.customenum.ResultEnum;
import com.ph.shopping.common.util.result.Result;
import com.ph.shopping.facade.mapper.PhAgentOrderMapper;
import com.ph.shopping.facade.mapper.PhAgentOrderProductsMapper;
import com.ph.shopping.facade.mapper.PhLogisticsForOderUseVoMapper;
import com.ph.shopping.facade.mapper.PhManagerOrderAddressMapper;
import com.ph.shopping.facade.mapper.PhMerchantOrderMapper;
import com.ph.shopping.facade.mapper.PhMerchantOrderProductsMapper;
import com.ph.shopping.facade.mapper.PhProductForOderUseVoMapper;

import cm.ph.shopping.facade.order.entity.PhAgentOrder;
import cm.ph.shopping.facade.order.entity.PhManagerOrderAddress;
import cm.ph.shopping.facade.order.entity.PhMerchantOrder;
import cm.ph.shopping.facade.order.service.IPhManagerOrderAddressService;
import cm.ph.shopping.facade.order.vo.PhAgentOrderProductsVo;
import cm.ph.shopping.facade.order.vo.PhLogisticsVo;
import cm.ph.shopping.facade.order.vo.PhManagerOrderAddressVo;
import cm.ph.shopping.facade.order.vo.PhMerchantOrderProductsVo;
import cm.ph.shopping.facade.order.vo.PhProductForOderUseVo;

/**
 * @项目：phshopping-servie_order
 *
 * @描述：商户收货地址 实现类
 *
 * 			@作者： yyg
 *
 * @创建时间：2017年3月24日 上午10:25:39
 *
 * @Copyright by yyg
 */
@Component
@Service(version = "1.0.0")
public class PhManagerOrderAddressServiceImpl extends BaseService implements IPhManagerOrderAddressService {

	@Autowired  //地址
	private PhManagerOrderAddressMapper phManagerOrderAddressMapper;
	
	@Autowired //商户订单下的商品
	private PhMerchantOrderProductsMapper phMerchantOrderProductsMapper;
	
	@Autowired
	 private PhMerchantOrderMapper   phMerchantOrderMapper;

	@Autowired 
	private PhProductForOderUseVoMapper  phProductForOderUseVoMapper;
	
	@Autowired
	private  PhLogisticsForOderUseVoMapper phLogisticsForOderUseVoMapper;
	
	@Autowired //代理商订单下的商品
	private PhAgentOrderProductsMapper phAgentOrderProductsMapper;
	
	
	@Autowired
	 private PhAgentOrderMapper   phAgentOrderMapper;
	
	@Override
	public List<PhManagerOrderAddressVo> findAddress(PhManagerOrderAddressVo addressVo) {
		return this.phManagerOrderAddressMapper.findAddress(addressVo);
	}
	
	
	@Override
	public Result addOrderAddress(PhManagerOrderAddress address) {
		int msg = phManagerOrderAddressMapper.insertUseGeneratedKeys(address);
		if (msg == 1) {
			return getResult(ResultEnum.SUCCESS, address.getId());
		} else {
			return getResult(ResultEnum.ADD_ORDER_ADDRESS_FAIL);
		}
	}
	
	@Override
	public  List<PhMerchantOrderProductsVo> findOrderProducts(PhMerchantOrderProductsVo orderProductsVo){
		return  this.phMerchantOrderProductsMapper.findOrderProducts(orderProductsVo);
	}
	@Override
	public  List<PhAgentOrderProductsVo> findAgentOrderProducts(PhAgentOrderProductsVo  agentOrderProductsVo){
		return  this.phAgentOrderProductsMapper.findAgentOrderProducts(agentOrderProductsVo);
	}
	
	/**
	 * 商户取消订单方法
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public Result   orderProductBack(PhMerchantOrder  order,List<PhProductForOderUseVo> list) throws Exception {
		int msg=0;
		msg=this.phMerchantOrderMapper.updateByPrimaryKeySelective(order);
		for (PhProductForOderUseVo phProductForOderUseVo : list) {
			msg=phProductForOderUseVoMapper.batchUpdateProductCont(phProductForOderUseVo);
		}
		
		if (msg == 1) {
			return getResult(ResultEnum.SUCCESS);
		} else {
			return getResult(ResultEnum.CANCEL_ORDER_DEFAULT_FAIL);
		}
	}
	
	/***
	 * 代理商取消订单方法
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public Result   agentOrderProductBack(PhAgentOrder  order,List<PhProductForOderUseVo> list) throws Exception {
		int msg=0;
		msg=this.phAgentOrderMapper.updateByPrimaryKeySelective(order);
		for (PhProductForOderUseVo phProductForOderUseVo : list) {
			msg=phProductForOderUseVoMapper.batchUpdateProductCont(phProductForOderUseVo);
		}
		if (msg == 1) {
			return getResult(ResultEnum.SUCCESS);
		} else {
			return getResult(ResultEnum.CANCEL_ORDER_DEFAULT_FAIL);
		}
	}
	
	
	

	@Override
	public List<PhLogisticsVo> findLogistics(PhLogisticsVo logisticsVo) {
		return 	this.phLogisticsForOderUseVoMapper.findLogistics(logisticsVo);
	}
	
	@Override
	public PhLogisticsVo	findLogisticsById(Long id){
		return  this.phLogisticsForOderUseVoMapper.findLogisticsById(id);
	}
}
